﻿using AdinZ.Common;
using Aliyun.OpenServices;
using Aliyun.OpenServices.OpenStorageService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Novel.Collection.WinForm
{
    public partial class Form1 : System.Windows.Forms.Form
    {

        Sys.BLL.novel_article bll_art = new Sys.BLL.novel_article();
        Sys.BLL.novel_chapter bll_chap = new Sys.BLL.novel_chapter();
        Novel.Sys.BLL.novel_article __book = new Sys.BLL.novel_article();

        private string novelName, novelAuthor;


        public Form1()
        {
            InitializeComponent();
            RichTextBox.CheckForIllegalCrossThreadCalls = false;
            
        }
        Thread thread = null;
        ClientConfiguration clientConfig = new ClientConfiguration();
        ObjectMetadata metadata = new ObjectMetadata();
        Novel.Sys.BLL.novel_chapter __bll_chap = new Sys.BLL.novel_chapter();

        #region 采集=============
        private void btnStartCoolection_Click(object sender, EventArgs e)
        {
            try
            {
                Constant.SID = int.Parse(tbStartCoolectionID.Text.Trim());
                Constant.EID = int.Parse(tbendCoolectionID.Text.Trim());
                if (Constant.EID < Constant.SID) { System.Windows.Forms.MessageBox.Show("结束ID不能小于开始ID"); return; }
            }
            catch { System.Windows.Forms.MessageBox.Show("开始或结束id错误"); return; }
            Constant.sqlConnStr = "Data Source=" + tbSqlHost.Text.Trim() + ";Initial Catalog=" + tbSqlName.Text.Trim() + ";Persist Security Info=True;User ID=" + tbSqlUser.Text.Trim() + ";Password=" + tbSqlPasswd.Text.Trim() ;
            tabControl1.SelectedIndex = 1;
            thread = new Thread(Strat);
            thread.IsBackground = true;
            thread.Start();
        }
        private void Strat()
        {
            for (int ID = Constant.SID; ID < Constant.EID+1; ID++)
            {
                WriteLog("开始采集【" + ID + "】.......\n");
                Sys.Model.novel_article model_art = new Sys.Model.novel_article();
                model_art=RegexData.CooleArt(NetServer.Get("http://www.qingdi.com/book/" + ID + ".html"));
                int chapterId=0;
                int Aid = 0;
                if (model_art != null) { WriteLog("\n\n采集到小说【"+model_art.Title+"】，作者【"+model_art.Author+"】\n"); }
                if (bll_art.Exists(model_art.Title, model_art.Author)) { /*已存在小说,获取小说id*/ 
                    Aid = bll_art.GetID(model_art.Title, model_art.Author);
                    WriteLog("小说【" + model_art.Title + "】，作者【" + model_art.Author + "】已存在\n");
                    chapterId = bll_chap.GetChapterNum(Aid);
                }
                else {

                    Aid = bll_art.Add(model_art);
                        model_art.ID = Aid;
                        if (!bll_art.ExistsCover(Aid))
                        {
                            object obj = (object)model_art;
                            Thread t = new Thread(UploadCover);
                            t.IsBackground = true;
                            t.Start(obj);
                        }
                    WriteLog("小说【" + model_art.Title + "】，作者【" + model_art.Author + "】成功添加到数据库中\n");
                }//添加小说

                if (Aid > 0)
                {
                    int chapterID = 0;
                    Dictionary<string, string> list = new Dictionary<string, string>();
                    list = RegexData.CooleChapter(NetServer.Get(model_art.InfoUrl));
                    string url = model_art.InfoUrl.Replace("index.html", "");
                    url = url.Replace("/html/", "/txt/");
                    if (list != null) { WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】采集了【" + list.Count + "】章\n"); }
                    foreach (KeyValuePair<string, string> item in list)
                    {

                        if (chapterID >= chapterId)
                        {
                            Sys.Model.novel_chapter model_chap = new Sys.Model.novel_chapter();
                            model_chap.Aid = Aid;
                            model_chap.Chaptername = item.Value;//本章标题
                            model_chap.Chapternum = chapterID + 1;//章ID
                            model_chap.Created = DateTime.Now;
                            model_chap.Isvip = 0;
                            model_chap.Modified = DateTime.Now;
                            model_chap.Price = 0;
                            model_chap.Size = 0;
                            model_chap.Status = 1;
                            model_chap.Articlepath = url + item.Key + ".txt";
                            model_chap.CollectionMark = item.Key.Trim();
                            if (!bll_chap.Exists(Aid, model_chap.Chaptername, item.Key.Trim()))
                            {
                                int chaptrNum;
                                novelName = model_art.Title;
                                novelAuthor = model_art.Author;
                                chaptrNum = bll_chap.Add(model_chap);
                                if (checkBox1.Checked)
                                {
                                    model_chap.ID = chaptrNum;
                                    object obj = (object)model_chap;
                                    Thread t = new Thread(UploadChapter);
                                    t.IsBackground = true;
                                    t.Start(obj);
                                }

                                WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】【" + item.Value + "】成功添加到数据库\n");
                            }
                        }
                        else { WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】【" + item.Value + "】已存在\n"); }
                        chapterID++;
                    }
                }
            }
        }


        private void WriteLog(string Log)
        {
            try
            {
                try
                {
                    tbCoolectionLog.AppendText(Log);
                }
                catch { }
            }
            catch (Exception e)
            {
                AdinZ.Common.WriteLog write = new AdinZ.Common.WriteLog("log/log.log");
                write.WriteLine(Log+"\r\n" + e + "\r\n\r\n");
            }
        }



        private void Strat1()
        {
            for (int ID = Constant.SID; ID < Constant.EID + 1; ID++)
            {
                WriteLog("开始采集【" + ID + "】.......\n");
                Sys.Model.novel_article model_art = new Sys.Model.novel_article();
                model_art = RegexData.CooleArt(NetServer.Get("http://www.39txt.com/txt/"+ID+".html"));
                int chapterId = 0;
                int Aid = 0;
                if (model_art != null) { WriteLog("\n\n采集到小说【" + model_art.Title + "】，作者【" + model_art.Author + "】\n"); }
                if (bll_art.Exists(model_art.Title, model_art.Author))
                { /*已存在小说,获取小说id*/
                    Aid = bll_art.GetID(model_art.Title, model_art.Author);
                    WriteLog("小说【" + model_art.Title + "】，作者【" + model_art.Author + "】已存在\n");
                    chapterId = bll_chap.GetChapterNum(Aid);
                }
                else
                {

                    Aid = bll_art.Add(model_art);
                    model_art.ID = Aid;
                    if (!bll_art.ExistsCover(Aid))
                    {
                        object obj = (object)model_art;
                        Thread t = new Thread(UploadCover);
                        t.IsBackground = true;
                        t.Start(obj);
                    }
                    WriteLog("小说【" + model_art.Title + "】，作者【" + model_art.Author + "】成功添加到数据库中\n");
                }//添加小说

                if (Aid > 0)
                {
                    int chapterID = 0;
                    Dictionary<string, string> list = new Dictionary<string, string>();
                    list = RegexData.CooleChapter(NetServer.Get(model_art.InfoUrl));
                    string url = model_art.InfoUrl.Replace("index.html", "");
                    url = url.Replace("/html/", "/txt/");
                    if (list != null) { WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】采集了【" + list.Count + "】章\n"); }
                    foreach (KeyValuePair<string, string> item in list)
                    {

                        if (chapterID >= chapterId)
                        {
                            Sys.Model.novel_chapter model_chap = new Sys.Model.novel_chapter();
                            model_chap.Aid = Aid;
                            model_chap.Chaptername = item.Value;//本章标题
                            model_chap.Chapternum = chapterID + 1;//章ID
                            model_chap.Created = DateTime.Now;
                            model_chap.Isvip = 0;
                            model_chap.Modified = DateTime.Now;
                            model_chap.Price = 0;
                            model_chap.Size = 0;
                            model_chap.Status = 1;
                            model_chap.Articlepath = url + item.Key + ".txt";
                            model_chap.CollectionMark = item.Key.Trim();
                            if (!bll_chap.Exists(Aid, model_chap.Chaptername))
                            {
                                int chaptrNum;
                                novelName = model_art.Title;
                                novelAuthor = model_art.Author;
                                chaptrNum = bll_chap.Add(model_chap);
                                if (checkBox1.Checked)
                                {
                                    model_chap.ID = chaptrNum;
                                    object obj = (object)model_chap;
                                    Thread t = new Thread(UploadChapter);
                                    t.IsBackground = true;
                                    t.Start(obj);
                                }

                                WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】【" + item.Value + "】成功添加到数据库\n");
                            }
                        }
                        else { WriteLog("当前采集ID【" + ID + "】小说【" + model_art.Title + "】，作者【" + model_art.Author + "】【" + item.Value + "】已存在\n"); }
                        chapterID++;
                    }
                }
            }
        }















        private void UploadCover(object obj)
        {
            Sys.Model.novel_article model = (Sys.Model.novel_article)obj;
            AdinZ.Common.DownloadFile downloadCover = new AdinZ.Common.DownloadFile();
            downloadCover.StrFileName = "cover/cover_" + model.ID + ".jpg";
            downloadCover.StrUrl = model.Cover;
            if (downloadCover.Download())
            {

                try
                {
                    using (var fs = File.Open(downloadCover.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                    {
                        metadata.ContentType = "image/jpeg";
                        if (AdinZ.Common.OssUpload.Upload(downloadCover.StrFileName, fs, metadata, clientConfig))
                        {
                            if (bll_art.UpdateCover(model.ID, downloadCover.StrFileName, 1) > 0)
                                WriteLog("小说【" + model.Title + "】，作者【" + model.Author + "】封面已经成功上传到OSS服务器中"+downloadCover.StrFileName+".\n");
                        }
                    }
                    File.Delete(downloadCover.StrFileName);
                }
                catch (Exception e)
                {
                    AdinZ.Common.WriteLog write = new AdinZ.Common.WriteLog("log/log.log");
                    write.WriteLine("出错了错误章节ID" + model.ID + "错误日志:" + e + "\r\n\r\n");
                }

            }
        }

        private void UploadChapter(object obj)
        {
            Sys.Model.novel_chapter model = (Sys.Model.novel_chapter)obj;
            AdinZ.Common.DownloadFile download = new AdinZ.Common.DownloadFile();
            download.StrFileName = "chapter/" + model.ID + ".txt";
            download.StrUrl = model.Articlepath;
            if (download.Download())
            {
                try
                {
                    using (var fs = File.Open(download.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                    {
                        metadata.ContentType = "text/plain";
                        if (AdinZ.Common.OssUpload.Upload(download.StrFileName, fs, metadata, clientConfig))
                        {
                            if (bll_chap.UpdateChapterPath(model.ID, download.StrFileName, 1,download.lDownloadFile) > 0)
                                WriteLog("小说【" + novelName + "】，作者【" + novelAuthor + "】【" + model.Chaptername + "】已经成功上传到OSS服务器中" + download.StrFileName + ".\n");
                        }
                    }
                    File.Delete(download.StrFileName);
                }
                catch (Exception e)
                {
                    AdinZ.Common.WriteLog write = new AdinZ.Common.WriteLog("log/log.log");
                    write.WriteLine("出错了错误章节ID" + model.ID + "错误日志:" + e + "\r\n\r\n");
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

#endregion


        #region 下载章节

        private void btnDownloadChapter_Click(object sender, EventArgs e)
        {
            lsDownloadChapter.Items.Clear();
            Thread t = new Thread(DownloadChapterStar);
            t.IsBackground = true;
            t.Start();
        }
        private void DownloadChapterStar()
        {

            while (true)
            {
                lsDownloadChapter.Items.Clear();
                DataSet ds = __bll_chap.GetListByPage("[local]=0  ", "id ASC", 0, 50);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string id = ds.Tables[0].Rows[i]["ID"].ToString();
                    string articlepath = ds.Tables[0].Rows[i]["articlepath"].ToString();
                    ListViewItem list = new ListViewItem(id);
                    list.SubItems.Add(articlepath);
                    lsDownloadChapter.Items.Add(list);
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    string chap_id = ds.Tables[0].Rows[i]["ID"].ToString();
                    string chap_Path = ds.Tables[0].Rows[i]["articlepath"].ToString();
                    string chaptername = ds.Tables[0].Rows[i]["chaptername"].ToString();

                    #region 下载、更新、修改、上传章节

                    DownloadFile df = new DownloadFile();
                    df.StrUrl = chap_Path;
                    df.StrFileName = "chapter/" + chap_id + ".txt";
                    if (File.Exists(df.StrFileName)) { File.Delete(df.StrFileName); }
                    if (df.Download())
                    {
                        try
                        {
                            using (var fs = File.Open(df.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                            {
                                metadata.ContentType = "text/plain";
                                if (OssUpload.Upload(df.StrFileName, fs, metadata, clientConfig))
                                {
                                    if (bll_chap.UpdateChapterPath(int.Parse(chap_id), df.StrFileName, 1, df.lDownloadFile) > 0)
                                        lsDownloadChapter.Items[i].SubItems[1].Text = df.StrFileName;
                                }
                            }
                            File.Delete(df.StrFileName);

                        }
                        catch (Exception e)
                        {
                            AdinZ.Common.WriteLog write = new WriteLog("log/log.log");
                            write.WriteLine("出错了错误章节ID" + chap_id + "错误日志:" + e + "\r\n\r\n");
                        }

                    }
                    #endregion

                }




                if (ds.Tables[0].Rows.Count + 1 < 50)
                {
                    Thread.Sleep(600000);
                    lsDownloadChapter.Items.Clear();
                    ListViewItem list = new ListViewItem("");
                    list.SubItems.Add("本次下载完了,休息10分钟在继续.....");
                    lsDownloadChapter.Items.Add(list);
                }
                else
                {
                    lsDownloadChapter.Items.Clear();
                    ListViewItem list = new ListViewItem("");
                    list.SubItems.Add("请稍等下载中........");
                    lsDownloadChapter.Items.Add(list);
                }
            }
        }
        #endregion



        #region 下载封面
        private void btnBookCaver_Click(object sender, EventArgs e)
        {
            lvBookCaver.Items.Clear();
            Thread t = new Thread(StartCaver);
            t.IsBackground = true;
            t.Start();

        }

        public void StartCaver()
        {
            int page = 1, size = 100;
            while (true)
            {
                DataSet ds = __book.GetListByPage("[local]=0", "", (page - 1) * size, page * size);
                lvBookCaver.Items.Clear();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string id = ds.Tables[0].Rows[i]["ID"].ToString();
                    string articlepath = ds.Tables[0].Rows[i]["cover"].ToString();
                    ListViewItem list = new ListViewItem(id);
                    list.SubItems.Add(articlepath);
                    lvBookCaver.Items.Add(list);
                }
               // return;

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    string bookID = ds.Tables[0].Rows[i]["ID"].ToString();
                    string bookCover = ds.Tables[0].Rows[i]["cover"].ToString();

                    #region 下载、更新、修改、上传章节

                    AdinZ.Common.DownloadFile downloadCover = new AdinZ.Common.DownloadFile();
                    downloadCover.StrFileName = "cover/cover_" + bookID + ".jpg";
                    downloadCover.StrUrl = bookCover;
                    if (downloadCover.Download())
                    {

                        try
                        {
                            using (var fs = File.Open(downloadCover.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                            {
                                metadata.ContentType = "image/jpeg";
                                if (AdinZ.Common.OssUpload.Upload(downloadCover.StrFileName, fs, metadata, clientConfig))
                                {
                                    if (bll_art.UpdateCover(int.Parse(bookID), downloadCover.StrFileName, 1) > 0)
                                    {
                                        lvBookCaver.Items[i].SubItems[1].Text = downloadCover.StrFileName;
                                    }
                                }
                            }
                            File.Delete(downloadCover.StrFileName);
                        }
                        catch (Exception e)
                        {
                        }

                    }

                    
                    
                    #endregion

                }



                if (ds.Tables[0].Rows.Count + 1 < size)
                {
                    Thread.Sleep(600000);
                }
                else
                {
                    lvBookCaver.Items.Clear();
                    ListViewItem list = new ListViewItem("");
                    list.SubItems.Add("请稍等........");
                    lvBookCaver.Items.Add(list);
                    page++;
                }
            }
        }


        #endregion

        private void btnDownloadFull_Click(object sender, EventArgs e)
        {

        }

        private void StartDownloadFull()
        {
            int page = 1; int pagesize = 50;
            while (true)
            {
                DataSet ds = __book.GetListByPage("[isdown]=0", "", (page - 1) * pagesize, page * pagesize);
                lsDownloadFull.Items.Clear();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListViewItem list = new ListViewItem(ds.Tables[0].Rows[i]["ID"].ToString());
                    list.SubItems.Add(ds.Tables[0].Rows[i]["title"].ToString());
                    list.SubItems.Add("等待中...");
                    lsDownloadFull.Items.Add(list);
                }

            }
        }
    }
}
